package com.nulldev.util.security.encryptAPI;

import com.nulldev.util.VariableAPI.ArrayUtils;
import com.nulldev.util.data.Base64.Base64;
import com.nulldev.util.manage.MarkedForRemoval;

@MarkedForRemoval(reason = "Replaced with the rewritten encryptAPI.", plannedRemovalWhen = "3.1")
public abstract class Encryptor {
	private final String id;
	protected final byte[] key;
	private final String[] provides;

	public Encryptor(final String id, final String[] provides, final String key) {
		this(id, provides, Base64.decodeBase64(key));
	}

	public Encryptor(final String id, final String[] provides, final byte[] key) {
		if (id == null)
			throw new NullPointerException("id == null!");
		else if (id.isEmpty())
			throw new IllegalArgumentException("id is empty");
		else if (key == null)
			throw new NullPointerException("key == null");
		else if (key.length == 0)
			throw new IllegalArgumentException("key is empty");
		else {
			this.id = id;
			this.key = key;
			this.provides = provides;
		}
	}

	/**
	 * Returns the byte[] of the encrypted data.
	 * 
	 * @param o (An object, will default to toString() call if unsupported)
	 * @returns Encrypted byte[]
	 */
	public abstract byte[] encryptToBytes(final Object o);

	/**
	 * Returns a Base64 string that is encrypted.
	 * 
	 * @param o (An object, will default to toString() call if unsupported)
	 * @returns Base64 String.
	 */
	public abstract String encrypt(final Object o);

	/**
	 * Decrypts a Base64 string that is encrypted.
	 * 
	 * @param s (An encrypted string)
	 * @returns Decrypted string.
	 */
	public abstract String decrypt(final String s);

	public abstract byte[] decryptToBytes(final byte[] b);

	public abstract byte[] getKey();

	public abstract String getKeyAsString();

	public String getEncryptorID() {
		return this.id;
	}

	public String[] getProvides() {
		return this.provides;
	}

	public boolean doesProvide(final String provider) {
		if (provider == null || provider.isEmpty()) {
			return false;
		}
		return ArrayUtils.contains(this.provides, provider);
	}
}
